<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
require('echarts/theme/macarons')
import resize from '@/mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'transform-mind'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = this.$echarts.init(this.$el, 'macarons')
      const data = {
        name: '低碳转型',
        children: [
          {
            name: '能源',
            symbolSize: 16,
            label: {
              fontSize: 16,
              color: '#008acd' //字体颜色
            },
            itemStyle: {
              color: '#0efcff', //这是节点折叠时候的颜色
              borderColor: '#54bcff',
              borderWidth: 3,
              normal: {
                color: '#00ff00'
              }
            },
            lineStyle: {
              color: '#008acd',
              width: 4,
              type: 'dotted' //'dotted'虚线 'solid'实线
            },
            children: [
              {
                name: '推行能源管理系统',
                value: 17010
              },
              {
                name: '鼓励多样化能源来源',
                value: 5842
              },
              {
                name: '使用绿色能源',
                children: [
                  {
                    name: '太阳能',
                    value: 1983
                  },
                  {
                    name: '风能',
                    value: 2047
                  },
                  {
                    name: '水能',
                    value: 1375
                  },
                  {
                    name: '地热能',
                    value: 8746
                  },
                  {
                    name: '生物质能',
                    value: 2202
                  }
                ]
              },
              {
                name: '加大新型能源研发',
                value: 1041
              },
              {
                name: '鼓励清洁能源使用',
                value: 5176
              },
              {
                name: '推行绿色能源激励机制',
                value: 449
              }
            ]
          },
          {
            name: '消费',
            category: 'tree2',
            label: {
              normal: {
                show: true
              }
            },
            itemStyle: {
              normal: {
                show: false,
                color: {
                  type: 'radial',
                  x: 0.5,
                  y: 0.5,
                  r: 0.5,
                  colorStops: [
                    {
                      offset: 0,
                      color: 'green' // 0% 处的颜色
                    },
                    {
                      offset: 1,
                      color: 'white' // 100% 处的颜色
                    }
                  ],
                  globalCoord: false // 缺省为 false
                },
                borderColor: 'blue',
                borderWidth: 2,
                shadowColor: 'rgba(0, 0, 0, 1)',
                shadowBlur: 10
              },
              emphasis: {
                color: {
                  type: 'radial',
                  x: 0.5,
                  y: 0.5,
                  r: 0.5,
                  colorStops: [
                    {
                      offset: 0,
                      color: 'green' // 0% 处的颜色
                    },
                    {
                      offset: 1,
                      color: 'white' // 100% 处的颜色
                    }
                  ],
                  globalCoord: false // 缺省为 false
                }
              }
            },
            children: [
              {
                name: '宣扬低碳生活方式',
                children: [
                  {
                    name: '绿色购物，自备布袋',
                    value: 721
                  },
                  {
                    name: '绿色出行，单车骑行',
                    value: 4294
                  },
                  {
                    name: '绿色金融，支持环保',
                    value: 9800
                  },
                  {
                    name: '绿色健康，杜绝暴食',
                    value: 1314
                  },
                  {
                    name: '绿色消费，节约用水',
                    value: 2220
                  }
                ]
              },
              {
                name: '建立绿色社区',
                value: 1759
              },
              {
                name: '加强建设绿色消费品标准',
                value: 2165
              }
            ]
          }
        ]
      }

      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          triggerOn: 'mousemove'
        },
        series: [
          {
            type: 'tree',
            initialTreeDepth: -1,
            data: [data],

            top: '1%',
            left: '7%',
            bottom: '1%',
            right: '20%',

            symbolSize: 10,

            lineStyle: {
              color: '#008acd'
            },
            itemStyle: {
              borderColor: '008acd'
            },
            label: {
              normal: {
                position: 'left',
                verticalAlign: 'middle',
                align: 'right',
                color: '#008acd'
              }
            },

            leaves: {
              label: {
                normal: {
                  position: 'right',
                  verticalAlign: 'middle',
                  align: 'left'
                }
              },
              itemStyle: {
                normal: {
                  color: {
                    type: 'radial',
                    x: 0.5,
                    y: 0.5,
                    r: 0.5,
                    colorStops: [
                      {
                        offset: 0,
                        color: 'green' // 0% 处的颜色
                      },
                      {
                        offset: 1,
                        color: 'blue' // 100% 处的颜色
                      }
                    ],
                    globalCoord: false // 缺省为 false
                  }
                }
              }
            }
          }
        ]
      })
    }
  }
}
</script>
